import { FilterXSS } from 'xss'

export const filterContent = (content) => {
  // 处理换行
  const options: any = {
    whiteList: {
      div: [],
      h1: [],
      p: [],
      ul: [],
      li: [],
      ol: [],
      dl: [],
      dd: [],
      dt: [],
      table: [],
      tr: [],
      td: [],
      caption: [],
      thead: [],
      tbody: [],
      form: [],
      textera: [],
      h2: [],
      h3: [],
      h4: [],
      h5: [],
      h6: [],
      br: []
    },
    onTag: (tag, name, currObj) => {
      if (currObj.isWhite) {
        if (name.indexOf('/') > -1) {
          return '\n'
        } else {
          return ''
        }
      } else {
        return name
      }
    }
  }
  const myxss = new FilterXSS(options)
  const html = myxss.process.bind(myxss)
  const returnHtml = html(content)
  // 处理行内标签
  const options1: any = {
    whiteList: {
      span: [],
      b: [],
      strong: [],
      i: [],
      em: [],
      u: [],
      ins: [],
      s: [],
      del: [],
      font: []
    },
    onTag: (tag, name, currObj) => {
      if (currObj.isWhite) {
        return ''
      } else {
        return name
      }
    }
  }
  const myxss1 = new FilterXSS(options1)
  const html1 = myxss.process.bind(myxss1)
  const returnHtml1 = html1(returnHtml)
  // 处理链接
  const options2: any = {
    whiteList: {
      a: [],
      img: []
    },
    onTag: (tag, name, currObj) => {
      const reg = /<(img|a)[^>]+(src|href)=['"]([^'"]+)['"]+/g
      let returnText: any = ''
      let temp: any = null
      while ((temp = reg.exec(name)) != null) {
        console.log(temp);
        returnText = temp[3]
      }
      if (returnText) {
        return returnText
      } else {
        return ''
      }
    }
  }
  const myxss2 = new FilterXSS(options2)
  const html2 = myxss.process.bind(myxss2)
  const returnHtml2 = html2(returnHtml1)
  return returnHtml2
}
